package com.ktg.mes.erpClient.service.impl;

import com.ktg.mes.erpClient.mapper.ErpCustomerMapper;
import com.ktg.mes.erpClient.service.IErpCustomerService;
import com.ktg.mes.md.domain.MdClient;
import com.ktg.mes.md.service.IMdClientService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IErpCustomerServiceImpl implements IErpCustomerService {
    private static final Logger log = LoggerFactory.getLogger(IErpCustomerServiceImpl.class);

    @Autowired
    private ErpCustomerMapper erpCustomerMapper;
    @Autowired
    private IErpCustomerService iErpCustomerService;

    @Autowired
    private IMdClientService iMdClientService;

    @Override
    public void syncDataListFromErp() {
        String lastId = "0";
        int count = 500;
        do {
            count = 0;

            // 处理 MesInventory 数据
            String maxPubfts = iMdClientService.queryMaxPubfts();
            if (StringUtils.isNotBlank(maxPubfts)) {
                lastId = maxPubfts;
            }

            List<MdClient> list = iErpCustomerService.queryListByLastPubufts(lastId);
            for (MdClient item : list) {
                MdClient dbItem = iMdClientService.selectMdClientByClientId(item.getClientId());
                if (dbItem != null && dbItem.getClientId() != null && dbItem.getClientId() > 0) {
                    // 不更新 inv_cat_code
                    iMdClientService.updateMdClient(item);
                }else {
                    iMdClientService.insertMdClient(item);
                }
            }
            count = list.size();
        }while (count == 500);

    }

    @Override
    public List<MdClient> queryListByLastPubufts(String pubufts) {
        return erpCustomerMapper.queryListByLastPubufts(pubufts);
    }


}
